package ch.subgate.chessvisualizationtrainer;

import ch.subgate.chessvisualizationtrainer.utils.Constants;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ChessVisualizationTrainerActivity extends Activity implements OnClickListener{
	
	private float mScore;
	private int mExercises;
	private float mTrainingTime;
	private int mBoardType;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SharedPreferences settings = getSharedPreferences(Constants.SHAREDPREF_FILE, 0);
        mScore = settings.getFloat("score", 0.0f);
        mExercises = settings.getInt("exercises", 0);
        mTrainingTime = settings.getFloat("training", 0.0f);
        mBoardType = settings.getInt("boardtype", 0);
        
        Button b = (Button)findViewById(R.id.onesquarecolor);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.twosquaresdiagonal);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.twosquaresknightmove);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.rkmate);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.bnmate);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.mateio);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.mateit);
        b.setOnClickListener(this);
    }
    
    /** Called when the activity stops. */
    @Override
    public void onStop() {
    	super.onStop();
    	
    	SharedPreferences settings = getSharedPreferences(Constants.SHAREDPREF_FILE, 0);
    	SharedPreferences.Editor editor = settings.edit();
    	editor.putFloat("score", mScore);
    	editor.putInt("exercises", mExercises);
    	editor.putFloat("training", mTrainingTime);
    	editor.putInt("boardtype", mBoardType);
    	
    	// Commit the edits
    	editor.commit();
    }
    
    @Override
    public void onClick(View v) {
    	// Preparing the intent
    	Intent i = new Intent(
    			ChessVisualizationTrainerActivity.this,
    			QuizActivity.class);
    	
    	i.putExtra("ch.subgate.chessvisualizationtrainer.score", mScore);
    	i.putExtra("ch.subgate.chessvisualizationtrainer.exercises", mExercises);
    	i.putExtra("ch.subgate.chessvisualizationtrainer.training", mTrainingTime);
    	i.putExtra("ch.subgate.chessvisualizationtrainer.boardtype", mBoardType);
    	
    	switch (v.getId()) {
    		case R.id.onesquarecolor:
    			// Start the one square color quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_ONESQUARECOLOR);
    			break;
    		case R.id.twosquaresdiagonal:
    			// Start the two squares diagonal quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_TWOSQUARESDIAGONAL);
    			break;
    		case R.id.twosquaresknightmove:
    			// Start the two squares knights move quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_TWOSQUARESKNIGHTSMOVE);
    			break;
    		case R.id.rkmate:
    			// Start the loaded rooks and queens mate quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_ROOKSQUEENSMATE);
    			break;
    		case R.id.bnmate:
    			// Start the loaded bishops and knights mate quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_BISHOPSKNIGHTSMATE);
    			break;
    		case R.id.mateio:
    			// Start the loaded mate in one quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_MATEINONE);
    			break;
    		case R.id.mateit:
    			// Start the loaded rooks and queens mate quiz
    			i.putExtra("ch.subgate.chessvisualizationtrainer.mode", Constants.MODE_MATEINTWO);
    			break;
    	}
    	
    	startActivityForResult(i, 0);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if (resultCode == RESULT_OK) {
    		mScore = data.getFloatExtra("ch.subgate.chessvisualizationtrainer.score", 0.0f);
        	mExercises = data.getIntExtra("ch.subgate.chessvisualizationtrainer.exercises", 0);
        	mTrainingTime = data.getFloatExtra("ch.subgate.chessvisualizationtrainer.training", 0.0f);
    	}
    }
}